package cn.gailvlun.gll.presentation.chat

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import cn.gailvlun.gll.R
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.ListRes
import cn.gailvlun.gll.net.Member
import cn.gailvlun.gll.net.apartment.member.BlockMemberReq
import cn.gailvlun.gll.presentation.base.App
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.chat.adapter.SilenceMemberAdapter
import cn.gailvlun.gll.presentation.xroom.adapter.NormalDialog
import cn.gailvlun.gll.util.DialogUtil
import cn.gailvlun.gll.util.RxUtil
import cn.gailvlun.gll.util.ToastUtil
import cn.gailvlun.gll.widget.OnCancelListener
import cn.gailvlun.gll.widget.OnConfirmListener
import com.afollestad.materialdialogs.MaterialDialog
import com.github.markzhai.recyclerview.BaseViewAdapter
import kotlinx.android.synthetic.main.activity_silence_member.*

class SilenceMemberActivity: BaseActivity() {

    companion object {
        fun openActivity(context: Context, roomId: Int) {
            context.startActivity(
                    Intent(context, SilenceMemberActivity::class.java)
                            .apply {
                                putExtra("roomId", roomId)
                            }
            )
        }
    }

    private var mAdapter: SilenceMemberAdapter? = null
    private var mLayoutManager: LinearLayoutManager? = null
    private var roomId: Int? = null

    override fun initComponent() {
        setContentView(R.layout.activity_silence_member)
    }

    override fun createHandler() {
        mAdapter = SilenceMemberAdapter(mContext)
        recycler_view.adapter = mAdapter
        mAdapter?.setPresenter(Presenter())

        mLayoutManager = LinearLayoutManager(mContext)
        recycler_view.layoutManager = mLayoutManager
    }

    override fun loadData(savedInstanceState: Bundle?) {
        roomId = intent.getIntExtra("roomId", -1).apply {
            if (this == -1) {
                finish()
                return
            }
        }

        refreshUI()
    }

    private fun refreshUI() {
        val pd = DialogUtil.showIndeterminate(mContext)
        HttpMethods.getMemberService().getMemberList(roomId!!, true, null, null, null)
                .compose(RxUtil.applyScheduler())
                .compose(bindToLifecycle())
                .subscribe(object: HttpSubscriber<ListRes<Member>>() {
                    override fun onSuccess(response: ListRes<Member>) {
                        mAdapter?.addMemberList(response.results)
                    }

                    override fun onFailure(errMsg: String?, response: ListRes<Member>?, code: Int) {
                        DialogUtil.showError(mContext, errMsg)
                    }

                    override fun onFinish() {
                        super.onFinish()

                        pd.dismiss()
                    }
                })
    }

    inner class Presenter : BaseViewAdapter.Presenter {
        fun silenceOrUnSilence(item: Member) {
            if (item.isBlock) {
                
                NormalDialog(mContext, "要解除${item.owner.name}的禁言吗？", null)
                        .apply {
                            onConfirmListener = object : OnConfirmListener<Any> {
                                override fun onConfirm(dialog: Dialog, data: Any?) {
                                    blockAndUnBlockUser(item)
                                    dismiss()
                                }
                            }
                            onCancelListener = object: OnCancelListener<Any> {
                                override fun onCancel(dialog: Dialog, data: Any?) {
                                    dismiss()
                                }

                            }
                        }
                        .show()
            } else {
                NormalDialog(mContext, "确定禁言${item.owner.name}吗？", "禁言后，房客无法在群内发言")
                        .apply {
                            onConfirmListener = object : OnConfirmListener<Any> {
                                override fun onConfirm(dialog: Dialog, data: Any?) {
                                    blockAndUnBlockUser(item)
                                    dismiss()
                                }
                            }
                            onCancelListener = object: OnCancelListener<Any> {
                                override fun onCancel(dialog: Dialog, data: Any?) {
                                    dismiss()
                                }

                            }
                        }
                        .show()
            }
        }

        private fun blockAndUnBlockUser(item: Member) {
            HttpMethods.getMemberService()
                    .blockMember(BlockMemberReq().apply { member_id = item.id })
                    .compose(RxUtil.applyScheduler())
                    .compose(bindToLifecycle())
                    .subscribe(object: HttpSubscriber<Member>() {
                        override fun onSuccess(response: Member?) {
                            refreshUI()
                        }

                        override fun onFailure(errMsg: String?, response: Member?, code: Int) {
                            DialogUtil.showError(mContext, errMsg)
                        }

                    })
        }
    }

    object Util {
        @JvmStatic
        fun getBlockText(item: Member): String {
            return if (item.isBlock) {
                "解除"
            } else {
                "禁言"
            }
        }

        @JvmStatic
        fun getBlockBG(item: Member): Drawable {
            return if (item.isBlock) {
                App.getInstance().resources.getDrawable(R.drawable.rounded_cool_grey)
            } else {
                App.getInstance().resources.getDrawable(R.drawable.rounded_apricot)
            }
        }

        @JvmStatic
        fun getGaiID(item: Member): String {
            return "概率论ID：${item.owner.gaiid}"
        }
    }

}